package com.gongpingjialoan.view;

import android.content.Context;
import android.content.Intent;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.gongpingjialoan.R;
import com.gongpingjialoan.activity.car.BuyCarVipActivity;
import com.gongpingjialoan.api.API;
import com.gongpingjialoan.api.Piont;
import com.gongpingjialoan.bean.LoanStatusData;
import com.gongpingjialoan.bean.VipBuycarBean;
import com.gongpingjialoan.manage.PointRecordManage;
import com.gongpingjialoan.network.NetDataJson;
import com.gongpingjialoan.utility.ArithUtil;
import com.gongpingjialoan.utility.DhUtil;
import com.gongpingjialoan.utility.JSONUtil;
import com.gongpingjialoan.utility.LoanUtil;

import org.json.JSONException;
import org.json.JSONObject;

/**
 * Created by Administrator on 2016/5/13.
 */
public class LoanCarModelView extends LinearLayout implements View.OnClickListener{
    Context mContext;
    private TextView permutation;
    private LinearLayout car_layout,select_car;
    private TextView brandT, modeldetailT;
    private String mCarPrice; //车辆预期价格
    public LoanCarModelView(Context context, AttributeSet attrs) {
        super(context, attrs);
        this.mContext = context;
        initView();
    }

    private void initView() {
        LayoutInflater.from(mContext).inflate(R.layout.view_loan_car_model, this);
        brandT = (TextView) findViewById(R.id.brand);
        modeldetailT = (TextView) findViewById(R.id.modeldetail);

        select_car = (LinearLayout) findViewById(R.id.select_car);
        permutation = (TextView) findViewById(R.id.permutation);
        car_layout = (LinearLayout) findViewById(R.id.car_layout);

        select_car.setOnClickListener(this);
        permutation.setOnClickListener(this);

        setCarInfos();
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.select_car:
                PointRecordManage.getInstance().addPoint(mContext, Piont.LoanApproveBrand);
                selectCarModel();
                break;
            case R.id.permutation:
                PointRecordManage.getInstance().addPoint(mContext, Piont.LoanApproveChange);
                selectCarModel();
                break;
        }
    }

    private void selectCarModel() {
        Intent intent = new Intent();
        intent.setClass(mContext, BuyCarVipActivity.class);
        intent.putExtra("noshow_time_and_note", false);
        intent.putExtra("from", "carlist");
        mContext.startActivity(intent);
    }

    private void setCarInfos() {
        String infos = LoanStatusData.getInstance().getInfos();
        if (TextUtils.isEmpty(infos) || infos.length() <= 2) {
            select_car.setVisibility(View.VISIBLE);
            car_layout.setVisibility(View.GONE);
        } else {
            select_car.setVisibility(View.GONE);
            car_layout.setVisibility(View.VISIBLE);
            try {
                JSONObject jo = new JSONObject(infos);
                mCarPrice = JSONUtil.getString(jo, "price");
                brandT.setText(JSONUtil.getString(jo, "model_zh"));
                modeldetailT.setText(DhUtil.getLoanAge(JSONUtil.getString(jo, "year")) + " | " + DhUtil.getLoanMile(JSONUtil.getString(jo, "mile")) + " | " + mCarPrice +"万元");
            } catch (JSONException e) {
                e.printStackTrace();
            }
        }
    }

    public void onBrandSelceted(VipBuycarBean vipBuycarBean) {
        String mBrandSlug = vipBuycarBean.getBrand_models().split(",")[0];
        String mModelSlug = vipBuycarBean.getBrand_models().split(",")[1];

        String mBrandName = vipBuycarBean.getBrand_models_zh().split(",")[0];
        String mModelName = vipBuycarBean.getBrand_models_zh().split(",")[1];
        String mMile = vipBuycarBean.getMile();
        String mYear = vipBuycarBean.getAge();
        if (mYear.contains("不限")) {
            mYear = "-";
        } else {
            mYear = "-" + vipBuycarBean.getAge();
        }
        mCarPrice = vipBuycarBean.getPrice();

        modeldetailT.setText(DhUtil.getLoanAge(mYear) + " | " + DhUtil.getLoanMile(mMile) + " | " + mCarPrice+"万元");

        brandT.setText(mModelName);
        select_car.setVisibility(View.GONE);
        car_layout.setVisibility(View.VISIBLE);

        JSONObject jo = new JSONObject();
        try {
            jo.put("brand", mBrandSlug);
            jo.put("brand_zh", mBrandName);
            jo.put("model", mModelSlug);
            jo.put("model_zh", mModelName);
            jo.put("price", mCarPrice);
            jo.put("year", mYear);
            jo.put("mile", mMile);

//            jo.put("modeldetail", mModelDetailSlug);
//            jo.put("modeldetail_zh", mModelDetailName);
        } catch (JSONException e) {
            e.printStackTrace();
        }
        uploadInfos(jo.toString());
    }

    private void uploadInfos(String infos) {
        NetDataJson netDataJson = new NetDataJson(new NetDataJson.OnStringListener() {
            @Override
            public void onDataJsonError(String errorMessage) {
                Toast.makeText(mContext, errorMessage, Toast.LENGTH_SHORT).show();
            }

            @Override
            public void onDataJsonUpdate(String json) {
                try {
                    JSONObject jo = new JSONObject(json);
                    if (jo == null) {
                        return;
                    }
                    if ("error".equals(jo.getString("status"))) {
                        Toast.makeText(mContext, jo.getString("msg"), Toast.LENGTH_SHORT).show();
                        return;
                    }
                    LoanUtil.toLoanData(json);
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }
        }, 1);

        netDataJson.setUrl(NetDataJson.LOAN_SERVER_DOMAIN + API.perfect_evaluations);
        netDataJson.setUseOtherDomain(true);
        netDataJson.addParam("step", "infos");
        netDataJson.addParam("infos", infos);
        netDataJson.request("put");
    }

    public int getCarPrice(){
        Double d = 0d;
        try {
            d = Double.valueOf(mCarPrice);
        }catch (Exception e){

        }
        int carPrice = 0;
        if (d != 0d){
            carPrice = ArithUtil.mul(d,10000d).intValue();
        }
        return carPrice;
    }
}
